<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>修改管理员</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="/static/js/jquery.min.js?v=2.1.4"></script>
    <script src="/static/js/base.js"></script>
    <link rel="stylesheet" href="/static/layui/css/layui.css" media="all">
    <script src="/static/layui/layui.2.7.6.js"></script>
</head>
<body>

<form class="layui-form" action=""  style="margin-top: 50px;" lay-filter="formFilter">
    <div class="layui-form-item">
        <label class="layui-form-label">登录名称</label>
        <div class="layui-input-block">
            <input type="text" name="user_name" lay-verify="user_name" autocomplete="off" disabled placeholder="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">密码</label>
        <div class="layui-input-block">
            <input type="password" name="password" lay-verify="password" autocomplete="off" placeholder="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">角色</label>
        <div class="layui-input-inline">

<!--            <input type="text" name="role_id" lay-verify="role_id" autocomplete="off" placeholder="" class="layui-input">-->
            <select id="role_id" name="role_id" lay-verify="required" lay-search="">
                <option value="">直接选择或搜索选择</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">真实姓名</label>
        <div class="layui-input-block">
            <input type="text" name="real_name" lay-verify="real_name" autocomplete="off" placeholder="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <input type="hidden" name="id" lay-verify="id" autocomplete="off" placeholder="" class="layui-input">
            <button type="submit" class="layui-btn" lay-submit="" lay-filter="lay-form">立即提交</button>
        </div>
    </div>
</form>

<script>

    $.ajax({
        url:'/admin/user/role',
        data:{status:1,limit:100},
        async:false,
        type:'get',
        dataType:'json',
        headers:{
            'token':localStorage.getItem('hypercmfname')+'_'+localStorage.getItem('token'),
            'accept':'*/*'
        },
        success:function(res){
            if(res.code==1){
                let html = ''
                for(let i of res.data.data){
                    html += `<option value="${i.id}">${i.role_name}</option>`
                }
                $("#role_id").append(html)
            }else{
                layer.alert(res.msg,{icon:5})//1√，2×，3问号，4锁，5不开心，6开心
            }
        },fail:function(res){
            layer.alert('请求失败',{icon:2})
        }
    })
    layui.use(['form', 'util', 'laydate'], function(){
        var form = layui.form;
        var layer =  parent.layui.layer;
        var util = layui.util;
        var laydate = layui.laydate;

        //日期
        laydate.render({
            elem: '#date'
        });
        laydate.render({
            elem: '#date1'
        });

        //自定义验证规则
        form.verify({
            role_name: function(value){
                if(value.length < 2){
                    return '标题至少得5个字符啊';
                }
            }
        });
        form.val('formFilter', {
            "id": decodeURI(getQueryVariable('id')),
            "user_name": decodeURI(getQueryVariable('user_name')),
            "real_name": decodeURI(getQueryVariable('real_name')),
            "role_id": decodeURI(getQueryVariable('role_id'))
        });
        //提交事件
        form.on('submit(lay-form)', function(data){
            console.log(JSON.stringify(data.field));

            $.ajax({
                url:'/admin/user/editAdmin',
                data:data.field,
                async:false,
                type:'post',
                dataType:'json',
                headers:{
                    'token':localStorage.getItem('hypercmfname')+'_'+localStorage.getItem('token'),
                    'accept':'*/*'
                },
                success:function(res){
                    if(res.code==1){
                        layer.msg('操作成功',{
                            icon: 1,
                            time: 1000 //1秒关闭（如果不配置，默认是3秒）
                        },function () {
                            layer.closeAll()
                            parent.layui.table.reload('dataTable')
                        })

                    }else{
                        layer.alert(res.msg,{icon:5})//1√，2×，3问号，4锁，5不开心，6开心
                    }
                },fail:function(res){
                    layer.alert('请求失败',{icon:2})
                }
            })
        });


    });
</script>

</body>
</html>